<template>
    <view class="container">
        <!-- 登录表单 -->
        <u--form :model="form" ref="loginForm">
            <u-form-item label="手机号" prop="phone">
                <u--input v-model="form.phone" placeholder="请输入手机号"></u--input>
            </u-form-item>
            <u-form-item label="密码" prop="password">
                <u--input type="password" v-model="form.password" placeholder="请输入密码"></u--input>
            </u-form-item>
        </u--form>

        <!-- 登录按钮 -->
        <u-button text="登录" type="primary" @click="handleLogin"></u-button>

        <!-- 注册跳转 -->
        <view class="link">
            <text @click="toRegister">没有账号？去注册</text>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                form: {
                    phone: '',
                    password: ''
                }
            }
        },
        methods: {
            handleLogin() {
                console.log('Attempting login with', this.form);
                uni.request({
                    url: 'http://localhost:8080/api/login',
                    method: 'POST',
                    data: this.form,
                    success: (res) => {
                        if (res.data.success) {
                            console.log('登录成功');
                            setTimeout(() => {
                                uni.reLaunch({
                                    url: '/pages/njt/user-register' // 👈 替换成你的主页路径
                                });
                            }, 1000);

                            // 跳转到首页或其他页面
                        } else {
                            console.error('登录失败:', res.data.message);
                        }
                    }
                });
            },
            toRegister() {
                uni.navigateTo({
                    url: '/pages/register/register'
                });
            }
        }
    }
</script>

<style scoped>
    .container {
        padding: 20px;
    }

    .link text {
        color: #007AFF;
        cursor: pointer;
    }
</style>